<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="250px" class="admin-sidebar">
        <div class="logo">
          <div class="logo-content">
            <h2>管理后台</h2>
            <el-button
              link
              size="small"
              @click="goToHome"
              class="home-btn"
            >
              <el-icon><HomeFilled /></el-icon>
              返回首页
            </el-button>
          </div>
        </div>

        <el-menu
          :default-active="activeMenu"
          class="admin-menu"
          router
          unique-opened
        >
          <el-menu-item index="/admin/dashboard">
            <el-icon><House /></el-icon>
            <span>控制台</span>
          </el-menu-item>

          <el-menu-item index="/admin/tables">
            <el-icon><Grid /></el-icon>
            <span>台球桌管理</span>
          </el-menu-item>

          <el-menu-item index="/admin/users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>

          <el-menu-item index="/admin/orders">
            <el-icon><Document /></el-icon>
            <span>订单管理</span>
          </el-menu-item>

          <el-menu-item index="/admin/inventory">
            <el-icon><Box /></el-icon>
            <span>库存管理</span>
          </el-menu-item>

          <el-menu-item index="/admin/reports">
            <el-icon><DataAnalysis /></el-icon>
            <span>财务报表</span>
          </el-menu-item>

          <el-menu-item index="/admin/activities">
            <el-icon><Bell /></el-icon>
            <span>活动公告</span>
          </el-menu-item>

          <el-menu-item index="/admin/settings">
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>

        <div class="sidebar-footer">
          <el-button type="danger" @click="logout" style="width: 100%">
            <el-icon><SwitchButton /></el-icon>
            退出登录
          </el-button>
        </div>
      </el-aside>

      <!-- 主内容区 -->
      <div class="main-container">
        <!-- 顶部导航 -->
        <div class="admin-header">
          <div class="header-left">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>

          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-info">
                <el-avatar :size="32" icon="el-icon-user-solid" />
                <span class="username">{{ userInfo.username }}</span>
                <el-icon><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人设置</el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>

        <!-- 主内容 -->
        <div class="admin-main">
          <router-view />
        </div>
      </div>
    </el-container>
  </div>
</template>

<script>
import { computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '../../store/modules/user';
import { ElMessage, ElMessageBox } from 'element-plus';
import {
  House,
  Grid,
  User,
  Document,
  Box,
  DataAnalysis,
  Bell,
  Setting,
  SwitchButton,
  ArrowDown,
  HomeFilled
} from '@element-plus/icons-vue';

export default {
  name: 'AdminLayout',
  components: {
    House,
    Grid,
    User,
    Document,
    Box,
    DataAnalysis,
    Bell,
    Setting,
    SwitchButton,
    ArrowDown,
    HomeFilled
  },
  setup() {
    const route = useRoute();
    const router = useRouter();
    const userStore = useUserStore();

    // 当前激活的菜单
    const activeMenu = computed(() => route.path);

    // 用户信息
    const userInfo = computed(() => userStore.userInfo || {});

    // 当前页面标题
    const currentPageTitle = computed(() => {
      const titleMap = {
        '/admin/dashboard': '控制台',
        '/admin/tables': '台球桌管理',
        '/admin/users': '用户管理',
        '/admin/orders': '订单管理',
        '/admin/inventory': '库存管理',
        '/admin/reports': '财务报表',
        '/admin/activities': '活动公告',
        '/admin/settings': '系统设置'
      };
      return titleMap[route.path] || '管理后台';
    });

    // 处理下拉菜单命令
    const handleCommand = (command) => {
      switch (command) {
        case 'profile':
          ElMessage.info('个人设置功能开发中...');
          break;
        case 'logout':
          logout();
          break;
      }
    };

    // 退出登录
    const logout = () => {
      ElMessageBox.confirm('确认退出登录？', '退出确认', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userStore.logout();
        router.push('/login');
        ElMessage.success('已退出登录');
      }).catch(() => {
        ElMessage.info('已取消退出');
      });
    };

    // 返回首页
    const goToHome = () => {
      router.push('/home');
    };

    return {
      activeMenu,
      userInfo,
      currentPageTitle,
      handleCommand,
      logout,
      goToHome
    };
  }
};
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.admin-sidebar {
  background: #304156;
  color: white;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
}

.logo {
  padding: 20px;
  border-bottom: 1px solid #434a50;
}

.logo-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.logo h2 {
  margin: 0;
  color: white;
  font-size: 18px;
}

.home-btn {
  color: #bfcbd9 !important;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  transition: all 0.3s;
}

.home-btn:hover {
  background-color: #263445 !important;
  color: #409eff !important;
}

.home-btn .el-icon {
  margin-right: 4px;
}

.admin-menu {
  flex: 1;
  border: none;
  background: transparent;
}

.admin-menu .el-menu-item {
  color: #bfcbd9;
  border-bottom: 1px solid #434a50;
}

.admin-menu .el-menu-item:hover,
.admin-menu .el-menu-item.is-active {
  background: #263445;
  color: #409eff;
}

.sidebar-footer {
  padding: 20px;
  border-top: 1px solid #434a50;
}

.main-container {
  margin-left: 250px;
  height: 100vh;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.admin-header {
  background: white;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  flex-shrink: 0;
  position: relative;
  z-index: 999;
}

.header-left {
  flex: 1;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background: #f5f7fa;
}

.username {
  color: #606266;
  font-size: 14px;
}

.admin-main {
  background: #f8fafc;
  padding: 0;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-sidebar {
    width: 200px !important;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .admin-sidebar.mobile-open {
    transform: translateX(0);
  }

  .main-container {
    margin-left: 0 !important;
  }

  .logo h2 {
    font-size: 16px;
  }

  .admin-header {
    padding: 0 15px;
  }
}

@media (max-width: 480px) {
  .admin-sidebar {
    width: 100% !important;
  }
}
</style>
